package com.khalil.kkadmin.module.ai.agent.chatsession;

import com.khalil.kkadmin.common.util.BoolTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class ChatSessionServiceImpl implements ChatSessionService {
    private final ChatSessionRepository chatSessionRepository;

    private final ChatSessionMessageRepository chatSessionMessageRepository;

    public ChatSessionServiceImpl(ChatSessionRepository chatSessionRepository, ChatSessionMessageRepository chatSessionMessageRepository) {
        this.chatSessionRepository = chatSessionRepository;
        this.chatSessionMessageRepository = chatSessionMessageRepository;
    }

    @Override
    public List<ChatSession> getUserApplicationChatSessionList(String username, Long applicationId) {
        return chatSessionRepository.findByUsernameAndApplicationIdOrderByCreatedAtDesc(username, applicationId);
    }

    @Override
    public List<ChatSessionMessage> getSessionMessageList(Long sessionId) {
        return chatSessionMessageRepository.findByChatSessionIdOrderByCreatedAtAsc(sessionId);
    }

    @Override
    public ChatSession getSession(Long sessionId) {
        return chatSessionRepository.findById(sessionId).orElse(null);
    }

    @Override
    public ChatSession createSession(String username, Long applicationId, String title) {
        ChatSession chatSession = new ChatSession();
        chatSession.setUsername(username);
        chatSession.setApplicationId(applicationId);
        chatSession.setTitle(title);
        return chatSessionRepository.save(chatSession);
    }

    @Override
    public void deleteSession(Long sessionId) {
        chatSessionRepository.deleteById(sessionId);
    }

    @Override
    public void deleteSessions(List<Long> sessionIds) {
        chatSessionRepository.deleteAllByIdInBatch(sessionIds);
    }

    @Override
    public void partialUpdateSession(Long sessionId, String property, Object value) {
        Optional<ChatSession> chatSessionOptional = chatSessionRepository.findById(sessionId);
        if (chatSessionOptional.isEmpty()) {
            log.warn("ChatSession not found");
            return;
        }
        ChatSession chatSession = chatSessionOptional.get();
        if (value == null) {
            log.warn("Value is null");
            return;
        }
        switch (property) {
            case "title":
                chatSession.setTitle((String) value);
                break;
            case "topFlag":
                chatSession.setTopFlag(BoolTool.isTrue((Boolean) value, false) ? 1 : 0);
                break;
            case "collectFlag":
                chatSession.setCollectFlag(BoolTool.isTrue((Boolean) value, false) ? 1 : 0);
                break;
            default:
                log.warn("Property not found");
                return;
        }
        chatSessionRepository.save(chatSession);
    }

    @Override
    public ChatSessionMessage createSessionMessage(Long sessionId, ChatSessionMessage sessionMessage) {
        sessionMessage = ChatSessionMessage.builder().chatSessionId(sessionId)
                .content(sessionMessage.getContent())
                .role(sessionMessage.getRole())
                .beforeId(sessionMessage.getBeforeId())
                .build();
        return chatSessionMessageRepository.save(sessionMessage);
    }
}
